package au.com.cgu.RescueU;
import java.util.ArrayList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class MongoBackend {	
	private static DB getDB() throws Exception {
		MongoClient mongoClient = new MongoClient("localhost", 27017);
        return mongoClient.getDB("mydb");
	}
	
	public static void resetDB() throws Exception {
		DBCollection table = getDB().getCollection("registrations");
		table.drop();
		DBCollection table2 = getDB().getCollection("policyvehicles");
		table2.drop();
	}

    public static void store_event(Registration reg) throws Exception {
	    DBCollection table = getDB().getCollection("registrations");
	    
	    BasicDBObject query = new BasicDBObject("regID", reg.RegistrationID());
	    DBCursor cursor = table.find(query);

	    if (cursor.hasNext()) {
	    	throw new Exception("Error: " + reg.RegistrationID() + " already in DB.");
	    } else {
	    	BasicDBObject document = new BasicDBObject();
			document.put("regID", reg.RegistrationID());
			document.put("PolicyHolder", reg.getPolicyHolder());
			document.put("PolicyNumber", reg.getPolicyNumber());
		   	table.insert(document);
	    }
    }
    
    public static Vehicle[] find_vehicles(String inPolicy) throws Exception {
    	ArrayList<Vehicle> list = new ArrayList<Vehicle>();
    	DBCollection table = getDB().getCollection("policyvehicles");
    	BasicDBObject query = new BasicDBObject("PolicyNo", inPolicy);
    	DBCursor cursor = table.find(query);
    	if (cursor.hasNext()) {
    		for (DBObject dbo : cursor) {
    			String RegNo = (String) dbo.get("RegNo");
    		    String VehName = (String) dbo.get("Name");
    		    String PolNo = (String) dbo.get("PolicyNo");
    		    Vehicle veh = new Vehicle(RegNo, VehName, PolNo);
    		    list.add(veh);
    		}
    	} else {
    		System.out.println("Nothing found for: " + inPolicy);
    	}
		return list.toArray(new Vehicle[list.size()]);
    }
    
    public static void store_event(Vehicle veh) throws Exception {
    	DBCollection table = getDB().getCollection("policyvehicles");
	    BasicDBObject query = new BasicDBObject("RegNo", veh.getRegistrationNumber());
	    DBCursor cursor = table.find(query);
	    if (cursor.hasNext()) {
	    	throw new Exception("Vehicle " + veh.getRegistrationNumber() + " has already been created");
	    } else {
	    	BasicDBObject document = new BasicDBObject();
			document.put("RegNo", veh.getRegistrationNumber());
			document.put("Name", veh.getName());
			document.put("PolicyNo", veh.getPolicyNumber());
		   	table.insert(document);
	    }
    }
    
    public static Registration find_registration(String inID) throws Exception {
    	Registration reg = null;
    	DBCollection table = getDB().getCollection("registrations");
    	BasicDBObject query = new BasicDBObject("regID", inID);
    	DBCursor cursor = table.find(query);
    	if (cursor.hasNext()) {
    		for (DBObject dbo : cursor) {
    			String regID = (String) dbo.get("regID");
    			String PolicyHolder = (String) dbo.get("PolicyHolder");
    			String PolicyNumber = (String) dbo.get("PolicyNumber");
    		    reg = new Registration(PolicyHolder, PolicyNumber, regID);
    		}
    	} else {
    		System.out.println("Nothing found for: " + inID);
    	}
		return reg;
    }
    
    public static void store_event(Claim claim) throws Exception {
	    DBCollection table = getDB().getCollection("claims");
	    BasicDBObject query = new BasicDBObject("ClaimNumber", claim.getClaimNumber());
	    DBCursor cursor = table.find(query);
	    if (cursor.hasNext()) {
	    	throw new Exception("Claim " + claim.getClaimNumber() + " has already been created");
	    } else {
	    	BasicDBObject document = new BasicDBObject();
			document.put("PolicyHolder", claim.getPolicyHolder());
			document.put("PolicyNumber", claim.getPolicyNumber());
			document.put("ClaimNumber", claim.getClaimNumber());
			
			Vehicle veh = claim.getVehicle();
			document.put("VehicleName", veh.getName());
			document.put("VehicleRegistration",  veh.getRegistrationNumber());
			
			
			
			
			document.put("claim", claim);
		   	table.insert(document);
	    }
    }
}
